﻿/// <reference path="jquery-1.7.1-vsdoc.js" />
/// <reference path="/Scripts/SiteScript.js" />
$(document).ready(function () {
    Initialize();
}
    );

//Model

var Form = new Object();//Global Form Model

var SectorModel = function (id, obligatoryFieldID, rightFieldID) {

    this.ID = String(id);
    this.ObligatoryFieldID = String(obligatoryFieldID);
    this.RightFieldID = String(rightFieldID);
    this.TextBoxArray = new Array();
    this.IsValid = new Boolean();

}

var TextBoxModel = function (id, clientID, stringWaterMark, isWaterMarked, validationType, isValid) {

    this.ID = String(id);
    this.ClientID = String(clientID);
    this.StringWaterMark = String(stringWaterMark);
    this.IsWaterMarked = Boolean(isWaterMarked);
    this.ValidationType = String(validationType);
    this.IsValid = Boolean(isValid);

}

//

//Initialize

function Initialize() {

    LoadControls();
    InitializeModel();
    AssignEventHandlers();

}

function InitializeModel() {

    Form.SectorID = new SectorModel("ID", "[id$=ObligatoryFieldID]", "[id$=RightFieldID]");
    Form.SectorContact = new SectorModel("Contact", "[id$=ObligatoryFieldContact]", "[id$=RightFieldContact]");
    Form.SectorMessage = new SectorModel("Message", "[id$=ObligatoryFieldMessage]", "[id$=RightFieldMessage]");

    Form.TextBoxName = new TextBoxModel("TextBoxName", "[id$=TextBoxName]", "Campo obligatorio | Apellido | Empresa", true,"NoNull", false);
    Form.TextBoxSurname = new TextBoxModel("TextBoxSurname", "[id$=TextBoxSurname]", "", false, "NoNull", false);
    Form.TextBoxCompany = new TextBoxModel("TextBoxCompany", "[id$=TextBoxCompany]", "", false, "NoNull", false);

    Form.SectorID.TextBoxArray[0] = Form.TextBoxName;
    Form.SectorID.TextBoxArray[1] = Form.TextBoxSurname;
    Form.SectorID.TextBoxArray[2] = Form.TextBoxCompany;

}

function LoadControls() {
   
    $(".FormCheckFields").css("display", "none");
    $(".FormRightField").css("display", "none");
    if ($(".FormMessageSended").css("display") != "none") {
        $(".FormMessageSended").fadeOut(3000);
    }
}

//

//Event Handlers

function AssignEventHandlers() {
    var i = 0;
    for (textbox in Form["SectorID"]["TextBoxArray"]) {//TODO: Utilizar un array de sectores en Form
        var textboxModel = Form["SectorID"]["TextBoxArray"][i];
        $(textboxModel["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorID"] ); });
        $(textboxModel["ClientID"]).change(function () { TextBox_Change(this, Form["SectorID"] ); });
        i++;
    }


}

function TextBox_Keyup(textbox, sector) {
    alert("asd");
    ValidTextBox(textbox, textboxModel);
    ValidSector(sector);

}


function TextBox_Change(textbox, sector) {

    ValidTextBox(textbox, textboxModel);
    ValidSector(sector);

}



//

//Validations

function ValidTextBox(textbox,textboxModel) {
    switch (textboxModel["ValidationType"]) {
        case "None":
            break;
        case "NoNull":
            if (!ValueIsNull($(textbox).val())) {

                Form[textboxModel["ID"]]["IsValid"] = true;
            }
            else {

                Form[textboxModel["ID"]]["IsValid"] = false;
            }
            break;
        case "Email":
            break;
        case "PhoneNumeric":
            break;
    }

}

function ValidSector(sector) {
    switch (sector["ID"]) {
        case "ID": //Logica: Valida que al menos uno de los textbox de este sector sea no nulo
            var isValid = false;
            var i = 0;
            for (textbox in Form["SectorID"]["TextBoxArray"]) {//TODO: Utilizar un array de sectores en Form
                var textboxModel = Form["SectorID"]["TextBoxArray"][i];
                isValid = isValid || textboxModel["IsValid"];
                i++;
            }
            if (isValid) {

            }
            else {

            }

            break;
    }
}

//







